<template>
  <a-card :bordered="false">
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="handleParam(record)">参数定义</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.tid)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>
      <j-modal
        :title="title"
        :width="width"
        :visible="visible"
        switchFullscreen
        @ok="handleOk"
        :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
        @cancel="handleCancel"
        cancelText="关闭">
        <a-button @click="handleParamAdd" type="primary" icon="plus">新增</a-button>
        <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="tid"
        :columns="columns_param"
        :dataSource="dataSourceParam"
        :pagination="pageIpagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="queryParamList">

        <span slot="isMust" slot-scope="text, record">
            <a-tag >{{ record.isMust==0?'否':'是' }}</a-tag>
        </span>

        <span slot="action" slot-scope="text, record">
          <a @click="handleParamEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleParamDelete(record.tid)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
      </j-modal>

    <collect-source-define-modal ref="modalForm" @ok="modalFormOk"></collect-source-define-modal>

    <c-s-param-modal ref="paramForm" @ok="paramFormOk"></c-s-param-modal>


  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin.js'
  import CollectSourceDefineModal from './modules/CollectSourceDefineModal'
  import { deleteAction, getAction,postAction, downFile, getFileAccessHttpUrl } from '@/api/manage'
  import CSParamModal from './modules/CSParamModal'
  import { deleteCollectSourceParamsDefine } from '@/api/apiDs'

  export default {
    name: 'CollectSourceDefineList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      CollectSourceDefineModal,
      CSParamModal
    },
    data () {
      return {
        description: '数据源定义表管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          // {
          //   title:'主键',
          //   align:"center",
          //   dataIndex: 'tid'
          // },
          {
            title:'数据源类型标识',
            align:"center",
            dataIndex: 'sourceCode'
          },
          {
            title:'数据源类型名称',
            align:"center",
            dataIndex: 'sourceName'
          },
          {
            title:'类加载',
            align:"center",
            dataIndex: 'classLoad'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        columns_param: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          // {
          //   title:'主键',
          //   align:"center",
          //   dataIndex: 'tid'
          // },
          {
            title:'参数code',
            align:"center",
            dataIndex: 'paramCode'
          },
          {
            title:'参数名称',
            align:"center",
            dataIndex: 'paramName'
          },
          {
            title:'是否是必须的',
            align:"center",
            dataIndex: 'isMust',
            scopedSlots: { customRender: 'isMust' }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/dcloud-platform/api/collectSourceDefine/v1/list",
          // list: "/sys/annountCement/listByUser",
          delete: "/dcloud-platform/api/collectSourceDefine/v1/delete",
          deleteBatch: "/dcloud-platform/api/collectSourceDefine/v1/deleteBatch",
          exportXlsUrl: "/dcloud-platform/api/collectSourceDefine/v1/exportXls",
          importExcelUrl: "com.dcloud.manage/collectSourceDefine/importExcel",

        },
        sourceId: '',
        dictOptions:{},
        superFieldList:[],
        title:'数据源参数',
        width:800,
        visible: false,
        disableSubmit: false,
        dataSourceParam:[],
        /* 分页参数 */
      pageIpagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'int',value:'tid',text:'主键',dictCode:''})
        fieldList.push({type:'string',value:'sourceCode',text:'数据源标识',dictCode:''})
        fieldList.push({type:'string',value:'sourceName',text:'数据源名称',dictCode:''})
        fieldList.push({type:'string',value:'classLoad',text:'类加载',dictCode:''})
        this.superFieldList = fieldList
      },
      handleOk () {
        this.close()
      },
      handleParam(record){
        console.log(record)
        this.visible = true
        this.sourceId = record.tid
        this.queryParamList()
      },
      handleParamEdit(record){
        this.$refs.paramForm.edit(record)
        this.$refs.paramForm.title = '编辑'
        this.$refs.paramForm.disableSubmit = false
      },
      handleParamAdd(){
        this.$refs.paramForm.add(this.sourceId)
        this.$refs.paramForm.title = '新增'
        this.$refs.paramForm.disableSubmit = false
      },

      //关闭弹窗
      handleCancel () {
        this.close()
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      queryParamList(){
        let params = {sourceTid:this.sourceId}
        this.loading = true
        getAction("/dcloud-platform/api/collectSource/ParamsDefine/v1/list", params)
          .then(res => {
            if (res.success) {
              //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
              this.dataSourceParam = res.result.records || res.result
              if (res.result.total) {
                this.pageIpagination.total = res.result.total
              } else {
                this.pageIpagination.total = 0
              }
              //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            } else {
              this.$message.warning(res.message)
            }
          })
          .finally(() => {
            this.loading = false
          })
      },
      paramFormOk(){
        this.queryParamList()
      },
      handleParamDelete(paramTid){
        const that = this
        let params = {id:paramTid}
        deleteCollectSourceParamsDefine(params).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.queryParamList()
            })
      }


    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>